package com.example.yichuangyun.ui.activity.follow;

import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.annotation.NonNull;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ActivitySearchBinding;
import com.example.yichuangyun.http.HttpRespond;
import com.example.yichuangyun.http.MyObserver;
import com.example.yichuangyun.http.RetrofitHelper;
import com.example.yichuangyun.model.business.FollowDataModel;
import com.example.yichuangyun.mvvm.base.BaseActivity;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.ui.adapter.FollowAdapter;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.ToastHelper;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 搜索跟进
 */
public class SearchFollowActivity extends BaseActivity<ActivitySearchBinding, BaseViewModel> {

    private List<FollowDataModel.FollowRecordModel> followData = new ArrayList<>();
    private FollowAdapter adapter;
    private int page = 1;

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void initData() {
        binding.etSearchContent.setHint(getString(R.string.call_search_my_business));
    }

    /**
     * 跟进记录
     */
    private void getFollowRecord(int page) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("keywords", binding.etSearchContent.getText().toString());
            jsonObject.put("page", page);
            jsonObject.put("pageSize", Constant.pageSize);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
        RetrofitHelper.getInstance().execute(RetrofitHelper.getService().getTabFollowLog(requestBody), new MyObserver<HttpRespond<FollowDataModel>>() {
            @Override
            protected void onSuccess(HttpRespond<FollowDataModel> followDataModelHttpRespond) {
                if (Constant.SUCCESS == followDataModelHttpRespond.code) {
                    if (followDataModelHttpRespond.data != null) {
                        if (followDataModelHttpRespond.data.getTotal() > 0) {
                            binding.llCount.setVisibility(View.VISIBLE);
                            binding.tvCount.setText(String.valueOf(followDataModelHttpRespond.data.getTotal()));
                        } else {
                            binding.llCount.setVisibility(View.GONE);
                        }
                        if (followDataModelHttpRespond.data.getRecords() != null) {
                            List<FollowDataModel.FollowRecordModel> list = followDataModelHttpRespond.data.getRecords();
                            if (list.size() == 0) {
                                binding.smartRefreshLayout.finishLoadMoreWithNoMoreData();
                            }
                            if (page == 1) {
                                followData = list;
                                adapter.onRefresh(list);
                                binding.smartRefreshLayout.finishRefresh();
                            } else {
                                adapter.addData(list);
                                binding.smartRefreshLayout.finishLoadMore();
                            }
                        }
                    }
                }
            }
        });
    }

    @Override
    public void initCtrl() {
        adapter = new FollowAdapter(context, followData, 0);
        binding.recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initClick() {
        binding.tvCancel.setOnClickListener(v -> finish());
        binding.etSearchContent.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                String content = binding.etSearchContent.getText().toString().trim();
                if (!TextUtils.isEmpty(content)) {
                    CommonUtil.closeKeyboard(SearchFollowActivity.this);
                    page = 1;
                    binding.recyclerView.scrollToPosition(0);
                    binding.smartRefreshLayout.autoRefresh();
                } else {
                    binding.llCount.setVisibility(View.GONE);
                    followData.clear();
                    adapter.onRefresh(followData);
                    ToastHelper.show(getString(R.string.please_follow_search_keywords));
                }
                return true;
            }
            return false;
        });

        binding.smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (!TextUtils.isEmpty(binding.etSearchContent.getText())) {
                    page++;
                    getFollowRecord(page);
                } else {
                    binding.smartRefreshLayout.finishLoadMoreWithNoMoreData();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                if (!TextUtils.isEmpty(binding.etSearchContent.getText())) {
                    page = 1;
                    getFollowRecord(page);
                } else {
                    binding.smartRefreshLayout.finishRefresh();
                }
            }
        });
    }
}